package com.poisonh.unifiedauth.dialog;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.ui.svprogresshud.SVProgressHUD;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.ToastUtils;

/**
 * 密码箱操作对话框
 * Created by PoisonH on 2016/7/6.
 */
public class PassWdDialogFragment extends BaseDialogFragment implements View.OnClickListener
{
    private TextView mTvCancel;
    private TextView mTvDelete;
    private TextView mTvLookEdit;

    private String mStrAccount;
    private String mStrPassWd;
    private String mStrUrl;
    private String mStrName;
    private String mStrId;
    private Activity mActivity;

    @Override
    public void onStart()
    {
        super.onStart();
        // 设置宽度为屏宽、靠近屏幕顶部。
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(0xff000000));
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(wlp);

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        window.setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);

        mStrAccount = getArguments().getString("ACCOUNT");
        mStrPassWd = getArguments().getString("PASSWORD");
        mStrUrl = getArguments().getString("URL");
        mStrName = getArguments().getString("NAME");
        mStrId = getArguments().getString("ID");
    }
//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
//    {
//        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
//        mActivity = getActivity();
//        View view = inflater.inflate(R.layout.layout_dialog_passwdedit, null);
//        return view;
//    }

    @Override
    protected View loadView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return inflater.inflate(R.layout.layout_dialog_passwdedit, null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        mActivity = getActivity();
        mTvLookEdit = (TextView) view.findViewById(R.id.tv_lookedit);
        mTvDelete = (TextView) view.findViewById(R.id.tv_delete);
        mTvCancel = (TextView) view.findViewById(R.id.tv_cancel);
        mTvLookEdit.setOnClickListener(this);
        mTvDelete.setOnClickListener(this);
        mTvCancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.tv_cancel:
                this.dismiss();
                break;
            case R.id.tv_delete:
                deletePassWdBox();
                this.dismiss();
                break;
            case R.id.tv_lookedit:
                ConstantUitls.HAVE = true;
                AddPasswordBoxDialogFragment boxDialogFragment = new AddPasswordBoxDialogFragment();
                Bundle mBundle = new Bundle();
                mBundle.putString("ACCOUNT", mStrAccount);
                mBundle.putString("PASSWORD", mStrPassWd);
                mBundle.putString("URL", mStrUrl);
                mBundle.putString("NAME", mStrName);
                mBundle.putString("ID", mStrId);
                boxDialogFragment.setArguments(mBundle);
                boxDialogFragment.show(getFragmentManager(), "");
                this.dismiss();
                break;
        }
    }

    /**
     * 条件删除密码箱
     */
    private void deletePassWdBox()
    {
        final DeleteListener listener = (DeleteListener) mActivity;
        SVProgressHUD.showWithStatus(getActivity(), "加载中...");
        HttpParams params = new HttpParams();
      //  String mStrUserId = PreferencesUtils.getString(getActivity(), "ID");
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        params.put("passBoxId", mStrId);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.DELETEPASSBOX, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                listener.onDeleteListener(true);
                ToastUtils.showToast(getActivity(), "删除数据成功");
                SVProgressHUD.dismiss(mActivity);
                dismiss();
            }

            @Override
            public void onFailure(String failReason)
            {
                listener.onDeleteListener(false);
                ToastUtils.showToast(mActivity, "删除数据失败");
                SVProgressHUD.dismiss(mActivity);
            }
        });
    }

    public interface DeleteListener
    {
        void onDeleteListener(boolean change);
    }
}
